#include "mainscene.h"
#include "ui_mainscene.h"
#include <QMessageBox>
#include <QDebug>
#include <QAction>
#include <QPainter>
#include <mypushbutton.h>
#include <QTimer>
#include <QSound>
MainScene::MainScene(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainScene)
{
    ui->setupUi(this);

    // 建立选择关卡的连接
    chooseLevelScene = new ChooseLevelScene();
    connect(chooseLevelScene,&ChooseLevelScene::chooseSceneBack,this,[=](){
        QTimer::singleShot(700,this,[=](){

            this->setGeometry(chooseLevelScene->geometry());
            chooseLevelScene->hide();
            this->show();
        });
    });
    // 建立创建关卡的连接
    connect(chooseLevelScene,&ChooseLevelScene::chooseLevel,this,[=](int num){
        QTimer::singleShot(700,this,[=](){
           chooseLevelScene->hide();
           playScene = new PlayScene(num);
           playScene->setGeometry(chooseLevelScene->geometry());
           playScene->show();
           connect(playScene,&PlayScene::chooseSceneBack,this,&MainScene::jumpChooseScene);
        });
    });

    // 实现点击开始按钮退出
    connect(ui->actionQuit,&QAction::triggered,this,[=](){
        // 弹出问题框 是否关闭
        QMessageBox::StandardButton flag = QMessageBox::question(this,"是否关闭？","你确定要退出吗？",QMessageBox::Yes | QMessageBox::No,QMessageBox::Yes);
        if(flag == QMessageBox::Yes)
            return this->close();
    });
    // 设置固定大小
    this->setFixedSize(320,588);
    // 设置标题图标
    setWindowIcon(QIcon(":/img/res/Coin0001.png"));

    // 设置标题
    setWindowTitle("翻转金币主场景");

    // 添加开始按钮
    MyPushButton * btn_start = new MyPushButton(":/img/res/MenuSceneStartButton.png");
    btn_start->setParent(this);
    btn_start->move(this->width()*0.5-btn_start->width()*0.5,this->height()*0.7);
    // 开始音效
    QSound * startSound = new QSound(":/img/res/TapButtonSound.wav",this);

    // 监听按钮
    connect(btn_start,&QPushButton::clicked,this,&MainScene::jumpChooseScene);
    connect(btn_start,&QPushButton::clicked,this,[=](){
        startSound->play();
    });
//    emit chooseLevelScene->chooseLevel(1);

}

MainScene::~MainScene()
{
    delete ui;
}

void MainScene::paintEvent(QPaintEvent *)
{
    // 背景绘制
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/img/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
    // 背景图标绘制
    pix.load(":/img/res/Title.png");
    pix = pix.scaled(pix.width()*0.5,pix.height()*0.5);
    painter.drawPixmap(10,30,pix);
}

void MainScene::jumpChooseScene()
{
    // 打开关卡
    // 延迟进入   1s后 执行某一个函数
    QTimer::singleShot(700,this,[=](){
        // 隐藏当前界面
        this->hide();
        chooseLevelScene->setGeometry(this->geometry());
        if(playScene){
            playScene->hide();
            chooseLevelScene->setGeometry(playScene->geometry());
            delete playScene;
            playScene = NULL;
        }
        // 展示选择关卡
        chooseLevelScene->show();

    });
}

